local skel = fk.CreateSkill {
  name = "emo__binfen",
  tags = {Skill.Limited},
}

Fk:loadTranslationTable{
  ["emo__binfen"] = "缤纷",
  [":emo__binfen"] = "限定技，出牌阶段，你可以选择任意名角色，令这些角色依次观看一个锁定技、限定技和出牌阶段限一次的主动技，其选择：1.获得其中一个技能并失去一个其他技能；2.回复一点体力。",

  ["#emo__binfen-gain"] = "缤纷：你可以获得一个新技能，替换一个已有技能",
  ["#emo__binfen-lose"] = "缤纷：选择你要失去的技能",
  ["#emo__binfen"] = "缤纷:选择任意名角色，其可以替换技能或摸牌",

  ["$emo__binfen1"] = "",
  ["$emo__binfen2"] = "",
}

skel:addEffect("active", {
  prompt = "#emo__binfen",
  min_target_num = 1,
  card_num = 0,
  target_filter = Util.TrueFunc,
  can_use = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
  end,
  on_use = function (self, room, use)
    local tos = use.tos
    room:sortByAction(tos)
    local map = { {}, {}, {} }
    for _, g in ipairs(Fk:getAllGenerals()) do
      for _, s in ipairs(g:getSkillNameList()) do
        local skill = Fk.skills[s]
        if skill and not skill:hasTag(Skill.AttachedKingdom) then
          if skill:hasTag(Skill.Compulsory, false) then
            table.insertIfNeed(map[1], s)
          elseif skill:hasTag(Skill.Limited) then
            table.insertIfNeed(map[2], s)
          elseif skill:isInstanceOf(ActiveSkill) and string.find(Fk:translate(":"..s), "出牌阶段限一次") then
            table.insertIfNeed(map[3], s)
          end
        end
      end
    end
    local exists = {}
    for _, p in ipairs(room.players) do
      table.insertTable(exists, p:getSkillNameList())
    end
    for _, m in ipairs(map) do
      for i = #m, 1, -1 do
        if table.contains(exists, m[i]) then
          table.remove(m, i)
        end
      end
    end

    for _, p in ipairs(tos) do
      if not p.dead then
        local choices = {}
        for _, m in ipairs(map) do
          if #m > 0 then
            table.insert(choices, table.random(m))
          end
        end
        local skills = p:getSkillNameList()
        local togain
        if #skills > 0 and #choices > 0 then
          togain = room:askToChoices(p, {
            choices = choices, skill_name = skel.name, min_num = 1, max_num = 1, cancelable = true,
            prompt = "#emo__binfen-gain", detailed = true,
          })[1]
          if togain then
            local tolose = room:askToChoice(p, {
              choices = skills, skill_name = skel.name, prompt = "#emo__binfen-lose",
            })
            room:handleAddLoseSkills(p, "-" .. tolose .. "|" .. togain)
            for _, m in ipairs(map) do
              table.removeOne(m, togain)
            end
          end
        end
        if not togain then
          room:recover { num = 1, skillName = skel.name, who = p, recoverBy = use.from }
        end
      end
    end
  end,
})

return skel
